<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"mjava.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":true},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="浅水是喧哗的，深水是沉默的">
<meta name="keywords" content="Java,Linux,Spring,Jenkins,Docker,RocketMQ,Vue">
<meta property="og:type" content="website">
<meta property="og:title" content="灰色World">
<meta property="og:url" content="http://mjava.top/page/4/index.html">
<meta property="og:site_name" content="灰色World">
<meta property="og:description" content="浅水是喧哗的，深水是沉默的">
<meta property="og:locale" content="zh-CN">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="灰色World">
<meta name="twitter:description" content="浅水是喧哗的，深水是沉默的">

<link rel="canonical" href="http://mjava.top/page/4/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-CN'
  };
</script>

  <title>灰色World</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="灰色World" type="application/atom+xml"><!-- hexo-inject:begin --><!-- hexo-inject:end -->
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>

<body itemscope itemtype="http://schema.org/WebPage">
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">灰色World</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/greycode9" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener external nofollow noreferrer" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content index posts-expand">
            
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://mjava.top/gof/gof-builder/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="灰灰">
      <meta itemprop="description" content="浅水是喧哗的，深水是沉默的">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="灰色World">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/gof/gof-builder/" class="post-title-link" itemprop="url">设计模式之建造者模式【用好玩的故事讲清楚设计模式】</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-27 06:50:31" itemprop="dateCreated datePublished" datetime="2019-12-27T06:50:31+00:00">2019-12-27</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-01-27 10:07:24" itemprop="dateModified" datetime="2021-01-27T10:07:24+00:00">2021-01-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/GOF/" itemprop="url" rel="index"><span itemprop="name">GOF</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><img src="http://cdn.mjava.top/20191226220204.jpg" alt></p>
<blockquote>
<p>积千里跬步,汇万里江河;每天进步一点点,终有一天将成大佬</p>
<p>所有源代码都在这:<a href="https://github.com/z573419235/GofDemo" rel="noopener external nofollow noreferrer" target="_blank">https://github.com/z573419235/GofDemo</a></p>
<p>各位大佬记得点个星星哦</p>
</blockquote>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>建造者模式用于实例化一个比较复杂的实体类,<font color="orange">当你实例化一个类时,它的构造参数比较多时,就可以用建造者模式来简化实例化过程</font>;前几篇工厂模式的文章我们说道买车,那只是简单的区工厂买车,我们不关系工厂是怎么造出来的.可是实际工厂造一辆车需要有方向盘、发动机、车架、轮胎等部件,而且不同品牌的车的部件都是不同的,<font color="orange">部件虽然不同,但是造车的方式基本都是差不多的步骤</font>,这时候就可以用建造者模式来造一辆车了;</p>
<p>建造者（Builder）模式由产品、抽象建造者、具体建造者、指挥者等 4 个要素构成</p>
<h2 id="土豪朋友开车厂"><a href="#土豪朋友开车厂" class="headerlink" title="土豪朋友开车厂"></a>土豪朋友开车厂</h2><p>​        土豪朋友上次买了车之后,发现造车卖还挺赚钱,于是决定涉足汽车领域,真是很有商业头脑啊,不愧是我的玉树临风,疯言疯语,语速惊人,人模狗样的土豪朋友啊.</p>
<p><img src="http://cdn.mjava.top/20191226171943.jpg" alt></p>
<p>一天,前去向他讨教汽车的知识,他给我讲了汽车的大致构成:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 汽车 产品类 定义汽车的构成</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 方向盘</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="keyword">private</span> String steering;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 发动机</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="keyword">private</span> String engine;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 车架</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="keyword">private</span> String frame;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 轮胎</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="keyword">private</span> String tire;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 展示一下汽车配置</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">show</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"&#123;"</span> +</span><br><span class="line">                <span class="string">"steering='"</span> + steering + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", engine='"</span> + engine + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", frame='"</span> + frame + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", tire='"</span> + tire + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">'&#125;'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>果真是大致啊,忽悠我不懂车是吧,就给我讲4个东西,这谁不知道啊,哼!土豪朋友忙解释到:这不是为了通俗易懂嘛!!哈哈哈—土豪朋友尴尬而不失礼貌的笑着!</p>
<p><img src="http://cdn.mjava.top/20191226172548.jpg" alt></p>
<p>算了算了,不和你计较了,你再和我讲下你们车厂的造车模式吧!!他说,一开始他造车自己累的半死,什么都要亲力亲为,后来招了两个专家,<font color="orange">一个负责宝马的制造,一个负责奔驰的制造</font>,我现在要什么车,只要指挥谁造车就好了.轻松的很;</p>
<p>他给我介绍了一下他的两个专家:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 宝马车建造者</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BMWBuilder</span> <span class="keyword">extends</span> <span class="title">AbstractBuild</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">buildEngine</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        car.setEngine(<span class="string">"宝马的发动机"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">buildSteering</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        car.setSteering(<span class="string">"宝马的方向盘"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">buildFrame</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        car.setFrame(<span class="string">"宝马的车架"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">buildTire</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        car.setTire(<span class="string">"宝马的轮胎"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 奔驰车建造者</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BenzBuilder</span> <span class="keyword">extends</span> <span class="title">AbstractBuild</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">buildEngine</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        car.setEngine(<span class="string">"奔驰的发动机"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">buildSteering</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        car.setSteering(<span class="string">"奔驰的方向盘"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">buildFrame</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        car.setFrame(<span class="string">"奔驰的车架"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">buildTire</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        car.setTire(<span class="string">"奔驰的轮胎"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><font color="orange">他们两个都遵循下面这个<code>AbstractBuild</code>汽车的建造规则:</font></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 抽象建造者 定义造车的方法</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AbstractBuild</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 造的产品是车</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="keyword">protected</span> Car car=<span class="keyword">new</span> Car();</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 造发动机</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">buildEngine</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 造轮胎</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">buildSteering</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 造车架</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">buildFrame</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 造轮胎</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">buildTire</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 得到造好的车</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Car <span class="title">getCar</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.car;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>土豪朋友还跟我讲了是怎么指挥他们造车的:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 所有的建造者要听这个包工头的话,叫你造什么就造什么</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Boss</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Car <span class="title">builderCar</span><span class="params">(AbstractBuild build)</span></span>&#123;</span><br><span class="line">        build.buildEngine();</span><br><span class="line">        build.buildFrame();</span><br><span class="line">        build.buildSteering();</span><br><span class="line">        build.buildTire();</span><br><span class="line">        <span class="keyword">return</span> build.getCar();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>经过他这一翻显摆之后,感觉虽然长的人摸狗样的,干起事来还真是一套一套的,哈哈哈哈!!</p>
<p><img src="http://cdn.mjava.top/20191226174032.gif" alt></p>
<p>说完,还向我展示了汽车是怎样造成的………..</p>
<p><img src="http://cdn.mjava.top/20191226174235.png" alt></p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>其实建造者模式和<font color="orange">工厂模式</font>还是挺像的,<font color="orange">建造者模式里的建造者就相当于工厂模式里的工厂</font>,不过建造者的核心是可以<font color="orange">控制顺序</font>,比如上面的土豪老板可以控制建造工人的建造顺序,可以控制他们是先造轮胎还是先造发动机,这才是建造者模式意义;</p>
<blockquote>
<p>建造者模式如果和<font color="orange">模板方法模式</font>搭配起来,<font color="orange">建造工人那个类封装一个模板方法</font>开放给老板,老板就可以直接控制这个类就可以了,那这就和工厂模式没什么两样了</p>
</blockquote>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://mjava.top/gof/gof-prototype/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="灰灰">
      <meta itemprop="description" content="浅水是喧哗的，深水是沉默的">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="灰色World">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/gof/gof-prototype/" class="post-title-link" itemprop="url">一个故事一个模式-原型模式</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-25 23:34:48" itemprop="dateCreated datePublished" datetime="2019-12-25T23:34:48+00:00">2019-12-25</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-01-27 10:07:24" itemprop="dateModified" datetime="2021-01-27T10:07:24+00:00">2021-01-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/GOF/" itemprop="url" rel="index"><span itemprop="name">GOF</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><img src="http://cdn.mjava.top/20191226084321.jpg" alt></p>
<blockquote>
<p>积千里跬步,汇万里江河;每天进步一点点,终有一天将成大佬</p>
<p>所有源代码都在这:<a href="https://github.com/z573419235/GofDemo" rel="noopener external nofollow noreferrer" target="_blank">https://github.com/z573419235/GofDemo</a></p>
<p>各位大佬记得点个星星哦</p>
</blockquote>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>​        前几天生病了,每天头昏脑胀的,诶,生病的时候才知道身体健康的重要性,以后还是要加强锻炼,身体是革命的本钱;</p>
<p>​        隔了差不多有五六天没写日志了,罪过罪过;好了,今天要说的是原型模式,原型模式在<code>Java</code>中核心秘密就是<code>clone</code>这个方法,通过重新<code>Object</code>中的<code>clone</code>方法.来达到原型模式;而要重新<code>clone</code>方法就必须要实现<code>Cloneable</code>这个接口,不实现这个接口的话就会报<code>java.lang.CloneNotSupportedException</code>异常;</p>
<h2 id="我是鸣人"><a href="#我是鸣人" class="headerlink" title="我是鸣人"></a>我是鸣人</h2><p>​        鸣人最喜欢的就是吃拉面,就算是上课的时候也是心心念念的想着一乐大叔的拉面</p>
<p><img src="http://cdn.mjava.top/20191225222050.gif" alt></p>
<p>先来看看鸣人的原型实体类:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zheng</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 我是鸣人实体类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Naruto</span> <span class="keyword">implements</span> <span class="title">Cloneable</span></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 姓名</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="keyword">private</span> String name=<span class="string">"鸣人"</span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 年龄</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age=<span class="number">13</span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 任务</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="keyword">private</span> String task;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *爱好</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="keyword">private</span> ArrayList&lt;String&gt; hobby=<span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 构造方法</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Naruto</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.hobby.add(<span class="string">"吃拉面"</span>);</span><br><span class="line">        <span class="keyword">this</span>.hobby.add(<span class="string">"泡温泉"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 重写Object类的clone方法</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Naruto <span class="title">clone</span><span class="params">()</span></span>&#123;</span><br><span class="line">        Naruto naruto=<span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            naruto=(Naruto)<span class="keyword">super</span>.clone();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (CloneNotSupportedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> naruto;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"Naruto&#123;"</span> +</span><br><span class="line">                <span class="string">"name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", age='"</span> + age + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", task='"</span> + task + <span class="string">'\''</span> +</span><br><span class="line">                <span class="string">", hobby="</span> + hobby +</span><br><span class="line">                <span class="string">'&#125;'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>为了代码整洁,我安装了lombok插件,所以不用写get/set方法,直接加个<code>@Data</code>注解就可以了;</p>
</blockquote>
<p>一天,鸣人上着伊鲁卡老师的课,可是心里还是念念不忘一乐大叔的拉面,想着前几天刚学了影分身之术,想着用分身术逃出去吃拉面.于是他就有变了一个分身留着这上课,自己却跑去吃拉面了;</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 原型模式</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zheng</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//我是鸣人本人</span></span><br><span class="line">        Naruto naruto=<span class="keyword">new</span> Naruto();</span><br><span class="line">        <span class="comment">//我是影分身</span></span><br><span class="line">        Naruto narutoYin=naruto.clone();</span><br><span class="line"></span><br><span class="line">        narutoYin.setTask(<span class="string">"上课"</span>);</span><br><span class="line">        naruto.setTask(<span class="string">"吃拉面"</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"鸣人本人:"</span>+naruto.toString());</span><br><span class="line">        System.out.println(<span class="string">"影分身:"</span>+narutoYin.toString());</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//控制台输出</span></span><br><span class="line">鸣人本人:Naruto&#123;name=<span class="string">'鸣人'</span>, age=<span class="string">'13'</span>, task=<span class="string">'吃拉面'</span>, hobby=[吃拉面, 泡温泉]&#125;</span><br><span class="line">影分身:Naruto&#123;name=<span class="string">'鸣人'</span>, age=<span class="string">'13'</span>, task=<span class="string">'上课'</span>, hobby=[吃拉面, 泡温泉]&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到,鸣人本人的任务是去<font color="orange">吃拉面</font>,他的影分身的任务是留着教室<font color="orange">上课</font>;当然鸣人可以通过他本人创建无数个影分身,同时执行多个任务;这就是<font color="orange">原型模式</font>;</p>
<p><img src="http://cdn.mjava.top/20191225222314.gif" alt></p>
<h2 id="浅拷贝和深拷贝"><a href="#浅拷贝和深拷贝" class="headerlink" title="浅拷贝和深拷贝"></a>浅拷贝和深拷贝</h2><p>原型模式就是通过一个原型clone出多个和原型一样的类,但是拷贝也分<font color="orange">浅拷贝</font>和<font color="orange">深拷贝</font>;</p>
<h3 id="浅拷贝"><a href="#浅拷贝" class="headerlink" title="浅拷贝"></a>浅拷贝</h3><blockquote>
<p>浅拷贝有多浅,浅到就相当于没有给你拷贝,他就是让你<font color="orange">和原型共用一个空间,没有给你分配新的内存</font>;</p>
</blockquote>
<p>比如上面的鸣人本人有爱好,但是隐分身一般是没有爱好的,所以创建隐分身要吧爱好给清除调:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 原型模式</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zheng</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//我是鸣人本人</span></span><br><span class="line">        Naruto naruto=<span class="keyword">new</span> Naruto();</span><br><span class="line">        <span class="comment">//我是影分身</span></span><br><span class="line">        Naruto narutoYin=naruto.clone();</span><br><span class="line"></span><br><span class="line">        narutoYin.setTask(<span class="string">"上课"</span>);</span><br><span class="line">        <span class="comment">//影分身不配有爱好</span></span><br><span class="line">        narutoYin.getHobby().clear();</span><br><span class="line">        naruto.setTask(<span class="string">"吃拉面"</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"鸣人本人:"</span>+naruto.toString());</span><br><span class="line">        System.out.println(<span class="string">"影分身:"</span>+narutoYin.toString());</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//控制台输出</span></span><br><span class="line">鸣人本人:Naruto&#123;name=<span class="string">'鸣人'</span>, age=<span class="string">'13'</span>, task=<span class="string">'吃拉面'</span>, hobby=[]&#125;</span><br><span class="line">影分身:Naruto&#123;name=<span class="string">'鸣人'</span>, age=<span class="string">'13'</span>, task=<span class="string">'上课'</span>, hobby=[]&#125;</span><br></pre></td></tr></table></figure>

<p>WTF,竟然把本人的爱好也清除调了,那还去吃啥拉面啊,算了算了,安安心心上课吧,诶;叫你上影分身课是时候不认真,失败了吧!!!</p>
<h3 id="深拷贝"><a href="#深拷贝" class="headerlink" title="深拷贝"></a>深拷贝</h3><blockquote>
<p>深拷贝就是在<code>clone</code>方法里除了克隆类之外,还要克隆引用对象,这样才会重新给引用对象<font color="orange">分配新的内存空间</font></p>
</blockquote>
<p>进过上次的教训,鸣人苦练影分身之术,终于学得核心所在,看看他新的影分身技能吧:</p>
<p><img src="http://cdn.mjava.top/20191225220944.png" alt></p>
<p>在变一个看看:</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 原型模式</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zheng</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//我是鸣人本人</span></span><br><span class="line">        Naruto naruto=<span class="keyword">new</span> Naruto();</span><br><span class="line">        <span class="comment">//我是影分身</span></span><br><span class="line">        Naruto narutoYin=naruto.clone();</span><br><span class="line"></span><br><span class="line">        narutoYin.setTask(<span class="string">"上课"</span>);</span><br><span class="line">        <span class="comment">//影分身不配有爱好</span></span><br><span class="line">        narutoYin.getHobby().clear();</span><br><span class="line">        naruto.setTask(<span class="string">"吃拉面"</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">"鸣人本人:"</span>+naruto.toString());</span><br><span class="line">        System.out.println(<span class="string">"影分身:"</span>+narutoYin.toString());</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//控制台输出</span></span><br><span class="line">鸣人本人:Naruto&#123;name=<span class="string">'鸣人'</span>, age=<span class="string">'13'</span>, task=<span class="string">'吃拉面'</span>, hobby=[吃拉面, 泡温泉]&#125;</span><br><span class="line">影分身:Naruto&#123;name=<span class="string">'鸣人'</span>, age=<span class="string">'13'</span>, task=<span class="string">'上课'</span>, hobby=[]&#125;</span><br></pre></td></tr></table></figure>

<p>哈哈,成功了,这下可以安安心心的区吃拉面了吧;</p>
<p><img src="http://cdn.mjava.top/20191225222402.gif" alt></p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>​        引用设计模式之禅的一句话:内部的数组和引用对象才不拷贝，其他的原始类型比如<code>int</code>、<code>long</code>、<code>char</code>等都会被拷贝，但是对于<code>String</code>类型，<code>Java</code>就希望你把它认为是基本类型，它是没有clone方法的，处理机制也比较特殊，通过字符串池（stringpool）在需要的时候才在内存中创建新的字符串，在使用的时候就把<code>String</code>当做基本类使用即可。注意:<font color="orange">使用clone方法，在类的成员变量上就不要增加final关键字,否则当你重新设置这个成员变量的值时是不能设置的,因为final的不可变的,只能引用原来的值</font></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://mjava.top/mysql/mysql-update-driver-connector-8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="灰灰">
      <meta itemprop="description" content="浅水是喧哗的，深水是沉默的">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="灰色World">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/mysql/mysql-update-driver-connector-8/" class="post-title-link" itemprop="url">更新驱动到mysql-connector-java-8遇到的一些问题</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-25 17:07:12" itemprop="dateCreated datePublished" datetime="2019-12-25T17:07:12+00:00">2019-12-25</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-01-27 10:07:24" itemprop="dateModified" datetime="2021-01-27T10:07:24+00:00">2021-01-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/MySQL/" itemprop="url" rel="index"><span itemprop="name">MySQL</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="更新驱动到mysql-connector-java-8遇到的一些问题"><a href="#更新驱动到mysql-connector-java-8遇到的一些问题" class="headerlink" title="更新驱动到mysql-connector-java-8遇到的一些问题"></a>更新驱动到mysql-connector-java-8遇到的一些问题</h1><h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><h3 id="POM"><a href="#POM" class="headerlink" title="POM"></a>POM</h3><figure class="highlight"><table><tr><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;mysql&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;8.0.16&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<h2 id="application-properties"><a href="#application-properties" class="headerlink" title="application.properties"></a>application.properties</h2><figure class="highlight properties"><table><tr><td class="code"><pre><span class="line"><span class="meta">spring.datasource.driver-class-name</span>=<span class="string">com.mysql.jdbc.Driver</span></span><br></pre></td></tr></table></figure>

<p>项目是<code>SpringBoot</code>构建的,数据库版本是:<code>MySQL5.7</code>,用了<code>mysql-connector-java-8</code>来链接数据库,<code>application.properties</code>也配置成<code>spring.datasource.driver-class-name=com.mysql.jdbc.Driver</code>,中间遇到了几个问题;</p>
<h3 id="问题一"><a href="#问题一" class="headerlink" title="问题一"></a>问题一</h3><h4 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h4><p>如上配置后,控制台报了一下错误:</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.</span><br></pre></td></tr></table></figure>

<p>翻译过来后就是:</p>
<p>加载类<code>com.mysql.jdbc.Driver</code>。 不推荐使用。 新的驱动程序类为<code>com.mysql.cj.jdbc.Driver</code>。 通过SPI自动注册驱动程序，通常不需要手动加载驱动程序类。</p>
<h4 id="解决"><a href="#解决" class="headerlink" title="解决"></a>解决</h4><p>根据提示,解决方法有<font color="orange">两种</font>:</p>
<ul>
<li>更改<code>application.properties</code>文件</li>
</ul>
<figure class="highlight properties"><table><tr><td class="code"><pre><span class="line"><span class="meta">spring.datasource.driver-class-name</span>=<span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="attr">//改成下面这样</span></span><br><span class="line"><span class="meta">spring.datasource.driver-class-name</span>=<span class="string">com.mysql.cj.jdbc.Driver</span></span><br></pre></td></tr></table></figure>

<ul>
<li>去掉<code>application.properties</code>文件中的<code>spring.datasource.driver-class-name</code>,因为它说会通过SPI自动注册的;</li>
</ul>
<h3 id="问题二"><a href="#问题二" class="headerlink" title="问题二"></a>问题二</h3><h4 id="描述-1"><a href="#描述-1" class="headerlink" title="描述"></a>描述</h4><p>数据库的数据时间总是和实际时间差8个小时</p>
<h4 id="解决-1"><a href="#解决-1" class="headerlink" title="解决"></a>解决</h4><p>在数据库url添加<code>serverTimezone=GMT%2B8</code></p>
<figure class="highlight properties"><table><tr><td class="code"><pre><span class="line"><span class="meta">spring.datasource.url</span>=<span class="string">jdbc:mysql://10.25.0.01:3307/db?useUnicode=true&amp;autoReconnect=true&amp;characterEncoding=UTF-8&amp;serverTimezone=GMT%2B8</span></span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p><code>mysql-connector-java</code>5.X的版本驱动名是:<code>com.mysql.jdbc.Driver</code>; 6.X及以上版本的驱动名是:<code>com.mysql.cj.jdbc.Driver</code></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://mjava.top/docker/docker-transfer-pit/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="灰灰">
      <meta itemprop="description" content="浅水是喧哗的，深水是沉默的">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="灰色World">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/docker/docker-transfer-pit/" class="post-title-link" itemprop="url">Docker迁移根目录导致mysql权限问题</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-20 15:04:16" itemprop="dateCreated datePublished" datetime="2019-12-20T15:04:16+00:00">2019-12-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-01-27 10:07:24" itemprop="dateModified" datetime="2021-01-27T10:07:24+00:00">2021-01-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Docker/" itemprop="url" rel="index"><span itemprop="name">Docker</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="问题描述"><a href="#问题描述" class="headerlink" title="问题描述"></a>问题描述</h2><p>　　最近由于公司服务器硬盘老是爆满，导致经常要清硬盘空间．最后发现/var/lib/docker目录占了25G,以前分kvm分区的时候，他们分了两个区：根目录＂/＂,和＂/home＂目录，发现home目录使用几乎为零，于是准备迁移Docker的根目录：</p>
<p>迁移根目录我看的是这个文章：<a href="https://www.cnblogs.com/insist-forever/p/11739207.html" rel="noopener external nofollow noreferrer" target="_blank">docker的存储目录迁移</a>,　不过迁移的时候我没有使用<font color="orange">rsync</font>这个命令，而是使用<font color="orange">cp -R</font>;</p>
<p>文件复制过去后，按照教程，重新启动docker服务，可是其中mysql容器跑不起来了，报mysqld: Can’t create/write to file ‘/tmp/ibTCv7Rw’ (Errcode: 13 - Permission denied)</p>
<p><img src="http://cdn.mjava.top/20191220143818.png" alt></p>
<p>期间按照网上的方法：说docker容器启动是添加–privileged=true,设置/tmp目录的权限，关闭selinux，这些方法<font color="orange">都没用！！！！！！</font></p>
<blockquote>
<p>其中设置/tmp文件权限这个方法，我把里面的/tmp文件挂载出来后，设置了权限，报这个的问题是解决了，可是又出现了新的问题，又报Version: ‘5.7.27’  socket: ‘/var/run/mysqld/mysqld.sock’ </p>
<p><img src="http://cdn.mjava.top/20191220144523.png" alt></p>
</blockquote>
<p>看来还是得从根源上解决问题啊！</p>
<h2 id="我的解决办法"><a href="#我的解决办法" class="headerlink" title="我的解决办法"></a>我的解决办法</h2><p>​        我想，既然是权限问题，那肯定是复制文件的时候权限丢失了，于是查了下cp命令保持权限的命令（cp -p）:</p>
<p><img src="http://cdn.mjava.top/20191220144841.png" alt></p>
<p>于是我又重新关闭的docker服务，然后删除了所有复制到home文件的目录，重新用<font color="orange">cp -p -R /var/lib/docker /home/docker/lib/</font>来重新复制了文件；</p>
<p>复制后，重启docker服务，启动docker容器，ok,一切正常；用docker info查看，看到已成功转移到/home下．<img src="http://cdn.mjava.top/20191220145849.png" alt></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://mjava.top/gof/gof-taemplate-method/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="灰灰">
      <meta itemprop="description" content="浅水是喧哗的，深水是沉默的">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="灰色World">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/gof/gof-taemplate-method/" class="post-title-link" itemprop="url">设计模式系列-模板方法模式</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-20 00:04:28" itemprop="dateCreated datePublished" datetime="2019-12-20T00:04:28+00:00">2019-12-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-01-27 10:07:24" itemprop="dateModified" datetime="2021-01-27T10:07:24+00:00">2021-01-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/GOF/" itemprop="url" rel="index"><span itemprop="name">GOF</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><img src="http://cdn.mjava.top/20191220000613.jpg" alt></p>
<blockquote>
<p>积千里跬步，汇万里江河．每天进步一点点，终有一天将成大佬</p>
</blockquote>
<h2 id="文前常规发言"><a href="#文前常规发言" class="headerlink" title="文前常规发言"></a>文前常规发言</h2><p>　　模板方法的设计<font color="orange">符合迪米特法则</font>，也就是最少知道原则，他通过对一些重复方法的封装，减少类之间的耦合，让调用者也更省心，原来要调两三个方法才能实现的功能，现在调一个就可以了；就像我们伟大的祖国，现在也在推行这种模式呢．以前区办一些证明什么的，要跑三四个地方，还要保证这三四个地方都正常帮你办理，如果其中一个地方没办理，那么整个流程就都作废了．现在好了，提倡最多跑一次，只要去一个地方办<font color="orange">一次</font>手续就可以了，你只要知道这个地方能办好就行，其他的就不用烦心了；</p>
<p><img src="http://cdn.mjava.top/20191219215431.gif" alt></p>
<h2 id="阿狗卖电脑"><a href="#阿狗卖电脑" class="headerlink" title="阿狗卖电脑"></a>阿狗卖电脑</h2><p>　　阿狗是一个三十五岁没了头发的年轻小伙，当问及为什么没了头发，阿狗摸摸头，眼里充满了悔恨的泪水；要不是小时候没听大人的话，长大了也不至于做程序员啊－－－阿狗唉声叹气的说道．听到这里，我仿佛已经知道了答案．当我问他为什么现在改行卖电脑了，他说外面的世界很大，想趁年轻，多闯闯（<font color="orange">实则是被公司裁员，被迫来卖电脑了</font>）；</p>
<p><img src="http://cdn.mjava.top/20191219220453.gif" alt></p>
<p>看看他的电脑店里都有什么</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 阿狗电脑店</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AGouShop</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *显卡</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">xianKa</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *cpu</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">cpu</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *电源</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">dianYuan</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *主板</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">zhuBan</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *硬盘</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">yingPan</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *内存条</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">neiCun</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *机箱</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">jiXiang</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>还不错，该有的都有了．当我们正在店里逛着时，来了两个顾客，<font color="orange">阿猫</font>和<font color="orange">大牛</font>，他们都来到阿狗店电脑店，挑选的电脑配件，准备组装电脑．</p>
<p>看看阿猫：</p>
<p><img src="http://cdn.mjava.top/20191219222853.png" alt></p>
<p>在看看大牛的：</p>
<p><img src="http://cdn.mjava.top/20191219222925.png" alt></p>
<p>再看看他们怎么组装的吧：</p>
<p><img src="http://cdn.mjava.top/20191219223122.png" alt></p>
<h2 id="有想法的阿狗"><a href="#有想法的阿狗" class="headerlink" title="有想法的阿狗"></a>有想法的阿狗</h2><p>　　阿狗自从卖电脑后，发现头上的头发也慢慢的长了出来了，每天也更加自信了．一天，他发现客户有个痛点，就是买电脑要分别买好配件，然后再自己组装，有时候买的配件有问题，又要拿去换，导致费时费力．这时，阿狗头脑灵光一闪，想到了当年做程序员时的<font color="orange">模板方法模式</font>；何不把客户组装电脑的步骤自己承包，这样客户只要来买电脑时选下<font color="orange">配件</font>，我就帮他组装好给他．客户省心省力，到时候生意肯定好；于是他改造了他的电脑店：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 阿狗电脑店</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AGouShop</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *显卡</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">xianKa</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *cpu</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">cpu</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *电源</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">dianYuan</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *主板</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">zhuBan</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *硬盘</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">yingPan</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *内存条</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">neiCun</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *机箱</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">jiXiang</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 阿狗帮客户装电脑</span></span><br><span class="line"><span class="comment">     * 模板方法</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">zhuZHuang</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"阿狗开始组装电脑＝＝＝＝＝＝"</span>);</span><br><span class="line">        <span class="keyword">this</span>.cpu();</span><br><span class="line">        <span class="keyword">this</span>.dianYuan();</span><br><span class="line">        <span class="keyword">this</span>.neiCun();</span><br><span class="line">        <span class="keyword">this</span>.xianKa();</span><br><span class="line">        <span class="keyword">this</span>.yingPan();</span><br><span class="line">        <span class="keyword">this</span>.zhuBan();</span><br><span class="line">        <span class="keyword">this</span>.jiXiang();</span><br><span class="line">        System.out.println(<span class="string">"阿狗电脑组装完成＝＝＝＝＝＝"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上次的阿猫又来买电脑了：</p>
<p><img src="http://cdn.mjava.top/20191219232821.png" alt></p>
<p>看看结果：</p>
<p><img src="http://cdn.mjava.top/20191219232931.png" alt></p>
<h2 id="客户反馈"><a href="#客户反馈" class="headerlink" title="客户反馈"></a>客户反馈</h2><p>　　阿狗按照上面的模式运行后，缺少增加了不少客户，可是有的顾客却反应说，为什么一定要我选显卡啊，我又不玩游戏，而且我买的cpu有核显，可以不要我选显卡嘛？阿狗一听，这是个问题啊，遵照客户就是上帝的原则(<font color="orange">有钱就赚原则</font>)，于是他又改了他店铺的模式：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 阿狗电脑店</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AGouShop</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *　显卡</span></span><br><span class="line"><span class="comment">     * ＂具体方法＂</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">xianKa</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"客户选了显卡"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 是否要显卡　　默认是要显卡的</span></span><br><span class="line"><span class="comment">     * ＂钩子方法＂</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isTrue</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *cpu</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">cpu</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *电源</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">dianYuan</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *主板</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">zhuBan</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *硬盘</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">yingPan</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *内存条</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">neiCun</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *机箱</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">jiXiang</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 阿狗帮客户装电脑</span></span><br><span class="line"><span class="comment">     * 模板方法</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">zhuZHuang</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"阿狗开始组装电脑＝＝＝＝＝＝"</span>);</span><br><span class="line">        <span class="keyword">this</span>.cpu();</span><br><span class="line">        <span class="keyword">this</span>.dianYuan();</span><br><span class="line">        <span class="keyword">this</span>.neiCun();</span><br><span class="line">        <span class="comment">//判断要不要显卡</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.isTrue()) &#123;</span><br><span class="line">            <span class="keyword">this</span>.xianKa();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.yingPan();</span><br><span class="line">        <span class="keyword">this</span>.zhuBan();</span><br><span class="line">        <span class="keyword">this</span>.jiXiang();</span><br><span class="line">        System.out.println(<span class="string">"阿狗电脑组装完成＝＝＝＝＝＝"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可以看到上加了<font color="orange">具体方法</font>和<font color="orange">钩子方法</font></p>
</blockquote>
<p>上上次的阿猫和大牛，又双来买电脑了－－－－－有钱真好：</p>
<p>阿猫默认要显卡：</p>
<p><img src="http://cdn.mjava.top/20191219234521.png" alt></p>
<p>大牛不要显卡：<img src="http://cdn.mjava.top/20191219234630.png" alt></p>
<p>看看他们的电脑吧：</p>
<p><img src="http://cdn.mjava.top/20191219234803.png" alt></p>
<h2 id="总结一下下"><a href="#总结一下下" class="headerlink" title="总结一下下"></a>总结一下下</h2><p>  上面对比了阿牛的三种买电脑模式</p>
<ul>
<li>普通模式：自己只提供最基础的东西，所有的由客户自己去完成</li>
<li>自己帮客户完成组装电脑：这里就用到了<font color="orange">模板方法模式</font>，通过对自身方法的封装，使客户买电脑更轻松了</li>
<li>客户有选择显卡的权利：这里用到了<font color="orange">模板方法模式</font>中的<font color="orange">钩子方法</font>，通过客户暴露钩子方法，使其可以控制阿狗在装电脑是要不要装显卡这个步骤方法，<font color="orange">钩子方法</font>是<font color="orange">模板方法模式</font>的灵魂，有了它，这个模式才有更大的意义；</li>
</ul>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://mjava.top/algorithm/array-vs-linked/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="灰灰">
      <meta itemprop="description" content="浅水是喧哗的，深水是沉默的">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="灰色World">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/algorithm/array-vs-linked/" class="post-title-link" itemprop="url">恍然大悟，数组和链表的区别</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-18 13:50:52" itemprop="dateCreated datePublished" datetime="2019-12-18T13:50:52+00:00">2019-12-18</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-01-27 10:07:24" itemprop="dateModified" datetime="2021-01-27T10:07:24+00:00">2021-01-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Algorithm/" itemprop="url" rel="index"><span itemprop="name">Algorithm</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><img src="http://cdn.mjava.top/20191218134901.jpg" alt></p>
<blockquote>
<p>积千里跬步，汇万里江河．每天进步一点点，终有一天将成大佬</p>
</blockquote>
<h2 id="文前发言"><a href="#文前发言" class="headerlink" title="文前发言"></a>文前发言</h2><p>　　在Java中，很多地方都使用了数组和链表，还有两种组合的叫<font color="orange">数组链表</font>结构，就是常说的<font color="orange">哈希表</font>，HashMap底层的数据结构就是哈希表．远了，远了，这里不讲HashMap,这里讲数组和链表；</p>
<p><img src="http://cdn.mjava.top/20191218104007.jpg" alt></p>
<h2 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h2><blockquote>
<p>数组是我们平时用的最多的数据结构，它的特点是<font color="orange">查询数据快，插入数据慢</font>，查询的时间复杂度是<font color="orange">O(1)</font>,插入的时间复杂度是<font color="orange">O(n)</font>.</p>
</blockquote>
<p>牛＊一族去学校读书，学校有四人寝和五人寝，大牛，二牛，三牛，四牛一同住进了四人寝里，每天都五缺一；有一天，他们在游戏里认识了小牛，得知小牛也是他们学校的，于是邀请小牛和他们一起住，可是他们们寝室只能住四个人，这个怎么办呢？于是他们向学校(<font color="orange">系统</font>)申请，要求学校给他们一个新的六人寝(<font color="orange">新的内存空间</font>)，于是学校就给了他们新的六人寝，于是他们全部都搬去了六人寝里，小牛也办了进去，之后每天五黑，好不快活；</p>
<p><img src="http://cdn.mjava.top/20191218113521.png" alt></p>
<blockquote>
<p>之后有其他学生看到牛＊他们的做法，于是也通通向学校申请；最后学校发现了一个问题：就是学生们为了住进新寝室，花费了大量的时间在从旧寝室到新寝室的路上(<font color="orange">插入数据慢</font>)</p>
</blockquote>
<p>有的人会说，那一开始就安排大牛，二牛，三牛，四牛住５人寝不就好了吗？这样他们就不用搬了(这就相当于我们初始化数组时，给数组指定了一个大小)；这样的想法是好的，但是如果他们没有没有认识小牛，小牛也不会搬进去，这样他们四个人就一直住着５人寝，就造成了空间资源浪费；</p>
<p>有一天，老师去找进入新寝室的小牛谈话，一看得知小牛在４号床，一下就找到了小牛（<font color="orange">查询数据快</font>），问他在这个寝室住的习不习惯，小牛心想，每天都五黑，你说我习不习惯！！</p>
<p><img src="http://cdn.mjava.top/20191218114115.gif" alt></p>
<h2 id="链表"><a href="#链表" class="headerlink" title="链表"></a>链表</h2><blockquote>
<p>链表我们平时用的比较少，它的特点是:<font color="orange">插入数据快，查询数据慢</font>，查询的时间复杂度是：<font color="orange">O(n)</font>，插入的时间复杂度是：<font color="orange">O(1)</font>，它的特点是和数组相反的；</p>
</blockquote>
<p>　　经过无数日夜的奋战，牛＊一寝人觉得是时候该出去玩玩了，自从小牛搬过来后，就一直没日没夜的五黑，都快不知道外面的世界长什么样子了；他们一行人准备去游乐园转转．</p>
<p>　　来到游乐园后，一群人像刚放出来的一样，对一切都充满了新鲜感，到处转悠．就在转悠的时候，细心的大牛发现了地上有一张纸条，打开一看，上面写着：<font color="orange">＂少年，你渴望力量吗？想获得力量就来海盗船找我！＂</font>，大牛赶紧找来其他小伙伴，一同前往；到了海盗船的地方，发现船上写着：<font color="orange">＂力量源自摩天轮，请前往摩天轮＂</font>，于是一群人就又前往摩天轮，在那里，终于过得了神秘力量－－－<font color="orange">毒鸡汤：你的内心有多强大，你的力量就有多强大</font>；小牛他们为了寻找这个力量，可谓费尽九牛二虎之力啊（<font color="orange">查询数据慢</font>）；</p>
<p><img src="http://cdn.mjava.top/20191218132932.png" alt></p>
<blockquote>
<p>可以发现，<font color="orange">每个元素存着下个元素的地址</font>，所以如果要查找其中某个元素，就必须要从头开始，才能找到．这就比较慢了．但是，他们<font color="orange">添加元素很快</font>,元素可以随机出现在游乐园的某个地方，只要在新添加元素的前一个元素指明新元素的地址在哪里就可以了；</p>
</blockquote>
<h2 id="发个对比表格吧"><a href="#发个对比表格吧" class="headerlink" title="发个对比表格吧"></a>发个对比表格吧</h2><h3 id="时间复杂度对比表"><a href="#时间复杂度对比表" class="headerlink" title="时间复杂度对比表"></a>时间复杂度对比表</h3><table>
<thead>
<tr>
<th align="center"></th>
<th align="center">数组</th>
<th>链表</th>
</tr>
</thead>
<tbody><tr>
<td align="center">插入</td>
<td align="center">O(n) 慢</td>
<td>O(1) 快</td>
</tr>
<tr>
<td align="center">删除</td>
<td align="center">O(n) 慢</td>
<td>O(1) 快</td>
</tr>
<tr>
<td align="center">查询</td>
<td align="center">O(1) 快</td>
<td>O(n) 慢</td>
</tr>
</tbody></table>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://mjava.top/gof/gof-abstract-factory/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="灰灰">
      <meta itemprop="description" content="浅水是喧哗的，深水是沉默的">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="灰色World">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/gof/gof-abstract-factory/" class="post-title-link" itemprop="url">设计模式系列-抽象工厂模式</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-16 21:17:23" itemprop="dateCreated datePublished" datetime="2019-12-16T21:17:23+00:00">2019-12-16</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-01-27 10:07:24" itemprop="dateModified" datetime="2021-01-27T10:07:24+00:00">2021-01-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/GOF/" itemprop="url" rel="index"><span itemprop="name">GOF</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><img src="http://cdn.mjava.top/20191216212929.jpg" alt></p>
<blockquote>
<p>积千里跬步，汇万里江河；每天进步一点点，终有一天将成大佬</p>
</blockquote>
<h2 id="突然开始的正文"><a href="#突然开始的正文" class="headerlink" title="突然开始的正文"></a>突然开始的正文</h2><p>紧接着上一章的<a href="https://mjava.top/gof/gof-factory-method">工厂方法模式</a>，其实抽象工厂的概念和工厂方法的概念都是差不多的，抽象工厂模式是对工厂方法模式的更高级，比如上次我们说的那个汽车工厂总部类<font color="orange">AllCarFactory</font>，本来他只定义了生产汽车这个方法，下面的各个品牌的汽车厂也只能生产这个汽车，现在由于市场需求，需要生产摩托车，然后<font color="orange">AllCarFactory</font>定义了一个生产摩托车的接口，这样这个接口下面的汽车厂就可以生产摩托车了．就在这时他们的生产模式也从<font color="orange">工厂方法模式</font>升级到了<font color="orange">抽象工厂模式</font>；</p>
<p>话不多说，看两个模式的类图你就明白了：</p>
<h3 id="原本的工厂方法模式类图："><a href="#原本的工厂方法模式类图：" class="headerlink" title="原本的工厂方法模式类图："></a>原本的工厂方法模式类图：</h3><p><img src="http://cdn.mjava.top/20191216202720.png" alt="工厂方法模式"></p>
<h3 id="升级后的抽象工厂模式："><a href="#升级后的抽象工厂模式：" class="headerlink" title="升级后的抽象工厂模式："></a>升级后的抽象工厂模式：</h3><p><img src="http://cdn.mjava.top/20191216203329.png" alt="抽象工厂模式"></p>
<p>可以看到，抽象工厂只是比工厂方法模式多生产了一个产品，当<font color="orange">抽象工厂模式</font>的产品减到<font color="orange">只有一个</font>的时候，他就又回到了<font color="orange">工厂方法模式</font>；</p>
<h2 id="好色的朋友买车了"><a href="#好色的朋友买车了" class="headerlink" title="好色的朋友买车了"></a>好色的朋友买车了</h2><p>上次我朋友看见我买车之后，得知是个小姐姐带我区买车的，于是他叫我联系了下那个小姐姐，说他也要买车，点名要叫小姐姐带他去，由于资金有限，他只卖了奔驰和五菱系列的产品，没有买莱斯莱斯的；看看他是怎么买的吧：</p>
<p><img src="http://cdn.mjava.top/20191216205803.png" alt></p>
<p>可以看到，由于要在一个工厂买两个东西，他是先找到了工厂，然后再一件一件的从工厂买．我们上次是一个工厂买一件东西，所以是直接去工厂买的；</p>
<h2 id="措不及防的结束了"><a href="#措不及防的结束了" class="headerlink" title="措不及防的结束了"></a>措不及防的结束了</h2><p><img src="http://cdn.mjava.top/20191216205427.gif" alt></p>
<p>不是我不想讲，而是抽象工厂就是这样的东西．从上面可以看出，抽象工厂每当增加一个产品时，后面相关的的<font color="orange">品牌工厂</font>也全部要实现他这个产品，这就违背了开闭原则了．所以，在实际设计中，一个<font color="orange">业务场景是稳定的</font>,用抽象工厂是比较好的，因为一次设计,后面就不用改了,这样就不会违反开闭原则了．但是如果一个<font color="orange">业务场景是稳定的</font>是不稳定的，那么就不适合使用这个模式了，因为后期需要多次修改，这就违反了开闭原则，同时也及其难维护，应为你不知道修改了代码，到底会影响哪些功能；</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://mjava.top/gof/gof-factory-method/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="灰灰">
      <meta itemprop="description" content="浅水是喧哗的，深水是沉默的">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="灰色World">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/gof/gof-factory-method/" class="post-title-link" itemprop="url">设计模式系列-工厂模式</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-15 17:25:00" itemprop="dateCreated datePublished" datetime="2019-12-15T17:25:00+00:00">2019-12-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-01-27 10:07:24" itemprop="dateModified" datetime="2021-01-27T10:07:24+00:00">2021-01-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/GOF/" itemprop="url" rel="index"><span itemprop="name">GOF</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><img src="http://cdn.mjava.top/gof-factory-method.jpg" alt></p>
<blockquote>
<p>积千里跬步，汇万里江河．每天进步一点点，终有一天将成大佬</p>
</blockquote>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>工厂模式有一下三种</p>
<ul>
<li>简单工厂模式</li>
<li>工厂方法模式</li>
<li>抽象工厂模式</li>
</ul>
<p>其中简单工厂模式不在23中模式之中，更多的是一种编程习惯，而我们平常所说的工厂模式一般指的是工厂方法模式，抽象工厂在实际的业务开发中也用的比较少，因为它有时候违背了开闭原则．由于篇幅有限，抽象工厂本文就不讲了，以后单独讲；</p>
<h2 id="简单工厂模式"><a href="#简单工厂模式" class="headerlink" title="简单工厂模式"></a>简单工厂模式</h2><p>简单工厂到底有多简单呢？简单到只有一个工厂，这个工厂相当于是万能工厂，你想要什么，只要和它说一声，它就会想方设法的去抱你创建，然后给你；举个买车的简单的例子：</p>
<p>当我要买车的时候，我选了这两种车．</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 创建一个汽车接口</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 汽车能动</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 奔驰车</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Benz</span> <span class="keyword">implements</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"大奔开动了"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 五菱神车</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Wuling</span> <span class="keyword">implements</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"五菱神车开动了"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>选是选好了，可是要怎么得到呢？是不是下意识的<font color="orange">new</font>一个出来？</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//我要奔驰车</span></span><br><span class="line">Benz　myCar=<span class="keyword">new</span> Benz();</span><br></pre></td></tr></table></figure>

<p>如果是这样的话，就相当于自己亲手造了一辆奔驰车出来，因为是你自己<font color="orange">new</font>出来的嘛！！！！！</p>
<p><img src="http://cdn.mjava.top/20191215162632.gif" alt></p>
<p>这种事情当然是交给工厂去做嘛，叫工厂去<font color="orange">new</font>就可以了，我只要交钱给工厂就可以了．诶，有钱真好！</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 汽车工厂</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 静态工厂</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 简单工厂</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CarFactory</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Car <span class="title">getCar</span><span class="params">(String type)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="string">"我要五菱神车"</span>.equals(type))&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> Wuling();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (<span class="string">"我要大奔驰"</span>.equals(type))&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> Benz();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>找到了这个工厂之后，我只要直接告诉它我要什么车就可以了，工厂就会帮我造好给我；</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 买车</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CostumerMain</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//跟车厂说一声我要五菱神车</span></span><br><span class="line">        Car wuling=CarFactory.getCar(<span class="string">"我要五菱神车"</span>);</span><br><span class="line">        <span class="comment">//跟车厂说一声我要大奔驰</span></span><br><span class="line">        Car Benz=CarFactory.getCar(<span class="string">"我要大奔驰"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//开着五菱神车去兜兜风</span></span><br><span class="line">        wuling.run();</span><br><span class="line">        <span class="comment">//开着大奔去兜兜风</span></span><br><span class="line">        Benz.run();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//五菱神车开动了</span></span><br><span class="line"><span class="comment">//大奔开动了</span></span><br></pre></td></tr></table></figure>

<p>这样子，买车就结束了，果然钱可以解决一切，哈哈，开个玩笑～</p>
<p><img src="http://cdn.mjava.top/20191215163319.jpg" alt></p>
<h2 id="工厂方法模式"><a href="#工厂方法模式" class="headerlink" title="工厂方法模式"></a>工厂方法模式</h2><p>上次买了两辆车之后，白天开着大奔去街上撩妹，晚上开着五菱神车去秋名山飙车，从此走向了人生巅峰．可是好景不长，大奔开着开着就漏油了，五菱神车终于也翻车了．</p>
<p><img src="http://cdn.mjava.top/20191215163907.gif" alt></p>
<p>找到了上次买车的工厂，准备换个低调点的劳斯莱斯．可是那家工厂竟然告诉我说他们那边还没有造过劳斯莱斯，需要改造一下工厂，然后才能生产劳斯莱斯，叫我等他们改造好之后再来买．听他们这麽说后，我心想，我这分分钟几百万上下的人，时间就是金钱．我可等不了．</p>
<p>于是几番寻找之后，发现英国有个劳斯莱斯车场，专门来生产劳斯莱斯．于是和接待我的中介小姐姐聊了一下，发现他们的生产模式是这样的：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 他们有个汽车工厂总部，用来定义车厂该干什么</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">AllCarFactory</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 生产汽车</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function">Car <span class="title">getCar</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">*　有个汽车规则，用来定义汽车能干什么</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    *　汽车能跑</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 劳斯莱斯汽车</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RollsRoyce</span> <span class="keyword">implements</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 劳斯莱斯能跑起来</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"劳斯莱斯开起来了！！"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 劳斯莱斯汽车工厂</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RollsRoyceFactory</span> <span class="keyword">implements</span> <span class="title">AllCarFactory</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 生产一辆劳斯莱斯</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Car <span class="title">getCar</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> RollsRoyce();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>找到车厂后，我毫不犹豫和接待我的小姐姐说给我来一辆，小姐姐见我这么豪爽，准备再忽悠我买几辆车，不推荐我几辆车．．．．她知道我之前买了奔驰和五菱神车,和我说他们这边还有还有五菱车厂和奔驰车厂，都是专门用来造同一种车的．于是我就去参观了一下：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* 五菱神车</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Wuling</span> <span class="keyword">implements</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 五菱神车能飙车</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"五菱神车开动了"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 五菱神车工厂</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WulingFactory</span> <span class="keyword">implements</span> <span class="title">AllCarFactory</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 生产一辆五菱神车</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Car <span class="title">getCar</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Wuling();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>再区看看奔驰车厂：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* 奔驰汽车</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Benz</span> <span class="keyword">implements</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 奔驰汽车能跑</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"大奔开动了"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 奔驰汽车工厂</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BenzFactory</span> <span class="keyword">implements</span> <span class="title">AllCarFactory</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 生产一辆奔驰汽车</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Car <span class="title">getCar</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Benz();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>看完之后，感觉还可以，于是分别到三个工厂买了三辆车，然后高高兴兴的回家了：</p>
<p><img src="http://cdn.mjava.top/20191215170620.jpg" alt></p>
<p>看看我买车的过程：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 土豪买车记</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CostumerMain</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//去五菱车厂买车</span></span><br><span class="line">        Car wuling=<span class="keyword">new</span> WulingFactory().getCar();</span><br><span class="line">        <span class="comment">//去奔驰车厂买车</span></span><br><span class="line">        Car benz=<span class="keyword">new</span> BenzFactory().getCar();</span><br><span class="line">        <span class="comment">//去劳斯莱斯车厂买车</span></span><br><span class="line">        Car rollsRoyce=<span class="keyword">new</span> RollsRoyceFactory().getCar();</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//开着三辆车去兜兜风</span></span><br><span class="line">        wuling.run();</span><br><span class="line">        benz.run();</span><br><span class="line">        rollsRoyce.run();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//五菱神车开动了</span></span><br><span class="line"><span class="comment">//大奔开动了</span></span><br><span class="line"><span class="comment">//劳斯莱斯开起来了！！</span></span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>​    买完车后，小姐姐还和我说他们这样的模式生产车的话有好多好处，比如一个车厂只要负责一种车的生产和售后，这样的话，生产效率就会比较高，赚的钱自然也多，同时每个车厂还可以举行不同活动，来吸引消费者，同时，你如果哪个品牌的车出现了问题了，直接去那辆车的工厂，基本上都能帮你解决问题，毕竟<font color="orange">术业有专攻</font>，对比前一个工厂什么都造的万金油来说，深入一项技术比什么技术都懂好；</p>
<p>​    不过有时候，万金油工厂也挺好的，就是一站式服务，你要什么它都有，不用到处乱跑，省心省力．所以还是要根据什么行业来执行什么模式，这样才能利益最大化；</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://mjava.top/gof/gof-oop-7-all/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="灰灰">
      <meta itemprop="description" content="浅水是喧哗的，深水是沉默的">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="灰色World">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/gof/gof-oop-7-all/" class="post-title-link" itemprop="url">OOP程序七大原则</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-15 10:45:04" itemprop="dateCreated datePublished" datetime="2019-12-15T10:45:04+00:00">2019-12-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-01-27 10:07:24" itemprop="dateModified" datetime="2021-01-27T10:07:24+00:00">2021-01-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/GOF/" itemprop="url" rel="index"><span itemprop="name">GOF</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><img src="http://cdn.mjava.top/autumn-4656877_1920.png" alt></p>
<h2 id="开闭原则"><a href="#开闭原则" class="headerlink" title="开闭原则"></a>开闭原则</h2><p>开闭原则相当于所有原则的祖先，主张对修改关闭，对拓展开放．</p>
<h2 id="里氏替换原则"><a href="#里氏替换原则" class="headerlink" title="里氏替换原则"></a>里氏替换原则</h2><p><font color="orange">当两个类有继承关系时，子类不能修改父类的方法和变量.  </font>里氏替换中的<font color="orange">替换</font>指的是：当有父类出现的地方，这个父类可以<font color="orange">替换</font>成子类，而且对程序没有影响，这就遵循了里氏替换原则；当替换成子类时对程序有影响，说明子类修改了父类的方法，就没有遵循里氏替换原则了；</p>
<h2 id="依赖倒置原则"><a href="#依赖倒置原则" class="headerlink" title="依赖倒置原则"></a>依赖倒置原则</h2><p>依赖倒置原则是对开闭原则的一个实现，也是主张对拓展开放，对修改关闭．它的核心思想是<font color="orange">面对接口编程，不要面对具体实现编程</font>．</p>
<p><img src="http://cdn.mjava.top/3-1Q113131610L7.gif" alt="来自C语言中文网"></p>
<p>这是一个遵守依赖倒置原则的UML图，原来的话当客户购买商品时,shopping这个方法要传入相应的网店进去，当要更改店铺时，就要修改Cusromer这个类里的shopping方法，而现在，只要定义一个Shop接口，所有的店铺都实现这个接口的方法，顾客类的shopping方法只要传入Shop这个接口类就可以了．然后具体实现的时候，要到哪里买，就传入哪一个网店就可以了，而不用修改Cusromer这个类的方法；</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//代码来之＇C语言中文网＇</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DIPtest</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Customer wang=<span class="keyword">new</span> Customer();</span><br><span class="line">        System.out.println(<span class="string">"顾客购买以下商品："</span>); </span><br><span class="line">        wang.shopping(<span class="keyword">new</span> ShaoguanShop()); </span><br><span class="line">        wang.shopping(<span class="keyword">new</span> WuyuanShop());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//商店</span></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">Shop</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">sell</span><span class="params">()</span></span>; <span class="comment">//卖</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//韶关网店</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ShaoguanShop</span> <span class="keyword">implements</span> <span class="title">Shop</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">sell</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"韶关土特产：香菇、木耳……"</span>; </span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//婺源网店</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">WuyuanShop</span> <span class="keyword">implements</span> <span class="title">Shop</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">sell</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"婺源土特产：绿茶、酒糟鱼……"</span>; </span><br><span class="line">    &#125;</span><br><span class="line">&#125; </span><br><span class="line"><span class="comment">//顾客</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Customer</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">shopping</span><span class="params">(Shop shop)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//购物</span></span><br><span class="line">        System.out.println(shop.sell()); </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//输出</span></span><br><span class="line">顾客购买以下商品：</span><br><span class="line">韶关土特产：香菇、木耳……</span><br><span class="line">婺源土特产：绿茶、酒糟鱼……</span><br></pre></td></tr></table></figure>

<h2 id="单一职责"><a href="#单一职责" class="headerlink" title="单一职责"></a>单一职责</h2><p><font color="orange">单一职责要求一个类只负责一项职责.  </font>这个听起来很简单，但是实际应用上却非常的难把握．因为这个职责在中国是非常抽象的概念，中国是一个文化底蕴非常丰富的国家，就像&lt;&lt;设计模式之禅&gt;&gt; 这本书里所说的例子：比如说中国的筷子，他既可以当刀来分割食物，也可以当叉子来叉取食物，而在国外，叉子就是叉子，用来取食物的，刀就是用来分割食物的；所以这个单一职责要求软件开发人员有非常丰富的实践经验．不然很难把握；</p>
<h2 id="迪米特法则"><a href="#迪米特法则" class="headerlink" title="迪米特法则"></a>迪米特法则</h2><p><font color="orange">迪米特法则也称最小知道原则，一个类对外暴露的东西越少越好．</font></p>
<ol>
<li>从依赖者的角度来说，只依赖应该依赖的对象。</li>
<li>从被依赖者的角度说，只暴露应该暴露的方法。</li>
</ol>
<p>个人理解：当A类需要调用B类的三个方法才能实现的功能时,B类可以对这三个方法进行一个封装，然后只暴露封装的这个方法给A,这样A就只需要调用B的这个封装的方法就可以了，当B的三个方法中有修改的时候，只要修改B这个对外封装的方法就可以，而Ａ调用者却不用改变，因为Ａ只知道调用这个方法可以实现功能，而不用具体管Ｂ内部是怎么实现的，降低了程序的耦合度；</p>
<h2 id="接口隔离原则"><a href="#接口隔离原则" class="headerlink" title="接口隔离原则"></a>接口隔离原则</h2><p>这个和单一职责有点类似，不过还是不一样的．</p>
<ul>
<li>单一职责原则注重的是职责，而接口隔离原则注重的是对接口依赖的隔离。</li>
<li>单一职责原则主要是约束类，它针对的是程序中的实现和细节；接口隔离原则主要约束接口，主要针对抽象和程序整体框架的构建。</li>
</ul>
<p>官方定义：<font color="orange">要求程序员尽量将臃肿庞大的接口拆分成更小的和更具体的接口，让接口中只包含客户感兴趣的方法，降低程序耦合度。</font></p>
<p>这个法则也要根据实际的业务场景来应用，如果粒度控制的太小，就会导致类的急剧增加，明明一个功能只要三四个类，如果粒度小的话，就会变成十几个，甚至几十个，虽然这样程序耦合度低，比较灵活，但是维护难啊．如果粒度大，耦合度就会高，程序不灵活．所以这个原则要求技术人员有足够的实践，经验和领悟；</p>
<h2 id="合成复用原则"><a href="#合成复用原则" class="headerlink" title="合成复用原则"></a>合成复用原则</h2><p>它要求在软件复用时，要尽量先使用组合或者聚合等关联关系来实现，其次才考虑使用继承关系来实现。如果要使用继承关系，则必须严格遵循<font color="orange">里氏替换原则</font>。合成复用原则同里氏替换原则相辅相成的，两者都是开闭原则的具体实现规范。</p>
<p>如果不了解什么是组合和聚合的话可以看看这个篇文章<a href="https://blog.csdn.net/qq_31655965/article/details/54645220" rel="noopener external nofollow noreferrer" target="_blank">&lt;&lt;组合、聚合与继承的爱恨情仇&gt;&gt;</a>,讲的挺好的</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>在程序设计中，尽量遵循OOP七大原则．不过有句话说的好，<font color="orange">规则是死的，人是活的</font>．意思是这七大原则有时候也不是万能的，有时候有的业务场景如果遵循了这些原则，反而变得难维护，所以一切都要从实际出发，23种设计模式也是一样，不要按死规则来．</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://mjava.top/jenkins/build-jenkins-sonarqube/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="灰灰">
      <meta itemprop="description" content="浅水是喧哗的，深水是沉默的">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="灰色World">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/jenkins/build-jenkins-sonarqube/" class="post-title-link" itemprop="url">Jenkins教程-集成SonarQube</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-13 15:38:02" itemprop="dateCreated datePublished" datetime="2019-12-13T15:38:02+00:00">2019-12-13</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-01-27 10:07:24" itemprop="dateModified" datetime="2021-01-27T10:07:24+00:00">2021-01-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Jenkins/" itemprop="url" rel="index"><span itemprop="name">Jenkins</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="什么是SonarQube"><a href="#什么是SonarQube" class="headerlink" title="什么是SonarQube?"></a>什么是SonarQube?</h2><p>看看维基百科的说明：<br><img src="http://cdn.mjava.top/Fi9bL7TAwFmCYPt3Cz_d0TIwm9rQ" alt></p>
<h3 id="SonarQube与CI-CD架构图"><a href="#SonarQube与CI-CD架构图" class="headerlink" title="SonarQube与CI/CD架构图"></a>SonarQube与CI/CD架构图</h3><p><img src="http://cdn.mjava.top/FpResgoc3tny2jvGqaqudPwCwstj" alt="SonarQube与CI/CD架构图"></p>
<h2 id="Docker运行SonarQube"><a href="#Docker运行SonarQube" class="headerlink" title="Docker运行SonarQube"></a>Docker运行SonarQube</h2><p>简单了解之后，开始安装SonarQube.这里用Docker安装</p>
<blockquote>
<p>注：这里用mysql来存储SonarQube的数据，SonarQube7.9起已经不在支持mysql了，可以安装官方推荐的PostgreSQL</p>
</blockquote>
<ul>
<li>SonarQube 6.7.7</li>
<li>Docker-CE 19.03.1</li>
<li>Mysql 5.7</li>
</ul>
<h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><p>直接运行这个docker命令来安装，网上其他的教程有什么挂载文件什么的，我试了都会安装失败，原因还是因为权限原因，因为SonarQube不是以root用户运行的，导致没权限读写挂载出来的文件夹．</p>
<blockquote>
<p>注意：创建容器前一定要先保证你连的容器有对应的数据库</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">docker run -d --name sonarqube -p 9099:9000 -p 9092:9092 --link=dev_mysql:mysql -e SONARQUBE_JDBC_USERNAME=app -e SONARQUBE_JDBC_PASSWORD=app -e SONARQUBE_JDBC_URL="jdbc:mysql://mysql:3306/sonar?useUnicode=true&amp;characterEncoding=utf8&amp;rewriteBatchedStatements=true&amp;useConfigs=maxPerformance&amp;useSSL=false" --restart=always sonarqube:6.7.7-community</span><br></pre></td></tr></table></figure>

<ul>
<li><p>–link=dev_mysql:mysql     这个命令我链接到了我的mysql容器，dev_mysql是容器的名字，mysql是在SonarQube容器里的别名，所以链接数据库时直接用mysql这个别名就可了．</p>
</li>
<li><p>SONARQUBE_JDBC_USERNAME ：数据库的账户</p>
</li>
<li><p>SONARQUBE_JDBC_PASSWORD ：数据库密码</p>
</li>
</ul>
<h3 id="访问"><a href="#访问" class="headerlink" title="访问"></a>访问</h3><p>安装好后直接访问<font color="orange">9099</font>端口，登录的账户和密码默认都是<font color="orange">admin</font>．首页就是这个样子的．</p>
<p><img src="http://cdn.mjava.top/Fs08WpcVDcL3n32MxoCNPDMtu1r5" alt></p>
<h2 id="Jenkins集成SonarQube"><a href="#Jenkins集成SonarQube" class="headerlink" title="Jenkins集成SonarQube"></a>Jenkins集成SonarQube</h2><p>Jenkins和SonarQube都是运行在Docker容器里的</p>
<h3 id="下载和安装插件"><a href="#下载和安装插件" class="headerlink" title="下载和安装插件"></a>下载和安装插件</h3><p>直接下载最新版的，然后导入，导入的方法可以看<a href="https://mjava.top/jenkins/problem-jenkins-01/">插件导入方法</a></p>
<ul>
<li>插件下载地址：<a href="https://updates.jenkins.io/download/plugins/sonar/" rel="noopener external nofollow noreferrer" target="_blank">https://updates.jenkins.io/download/plugins/sonar/</a></li>
</ul>
<h3 id="SonarQube生成Token"><a href="#SonarQube生成Token" class="headerlink" title="SonarQube生成Token"></a>SonarQube生成Token</h3><p>进入SonarQube管理界面</p>
<p>Administration-&gt;Security-&gt;Users</p>
<p><img src="http://cdn.mjava.top/FpFSYEgJfsJIwgNMA6tHHZtdAtpV" alt></p>
<p>然后随便输入一个名字，点击生成，记下Token</p>
<p><img src="http://cdn.mjava.top/FhTOSglZYOrP5poo_mmR3SGoobsD" alt></p>
<h3 id="添加全局凭证"><a href="#添加全局凭证" class="headerlink" title="添加全局凭证"></a>添加全局凭证</h3><p>类型选Secret text,然后Secret和ID输入框都填入刚才生成的Token</p>
<p><img src="http://cdn.mjava.top/FrKo5EjJ9-78uYbDjBpiuVkeu-_5" alt></p>
<h3 id="设置SonarQube-servers"><a href="#设置SonarQube-servers" class="headerlink" title="设置SonarQube servers"></a>设置SonarQube servers</h3><p>进入　系统管理-&gt;系统设置-&gt;SonarQube servers　　　　<font color="orange">设置好后点保存</font></p>
<blockquote>
<p>因为我SonarQube和Jenkins安装在同一台机器不同的Docker容器里的,所以这里URL直接填SonarQube的Docker容器的IP和端口</p>
</blockquote>
<p><img src="http://cdn.mjava.top/FpevTpJePMDg9-HSoQoSOCNORHL8" alt></p>
<h3 id="安装SonarQube-Scanner"><a href="#安装SonarQube-Scanner" class="headerlink" title="安装SonarQube Scanner"></a>安装SonarQube Scanner</h3><h4 id="下载压缩包"><a href="#下载压缩包" class="headerlink" title="下载压缩包"></a>下载压缩包</h4><ul>
<li>下载SonarQube Scanner压缩包：<a href="https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.2.0.1873-linux.zip" rel="noopener external nofollow noreferrer" target="_blank">SonarQube Scanner</a></li>
</ul>
<h4 id="解压到Jenkins挂载出来的目录里"><a href="#解压到Jenkins挂载出来的目录里" class="headerlink" title="解压到Jenkins挂载出来的目录里"></a>解压到Jenkins挂载出来的目录里</h4><p>只有解压到挂载出来的Jenkins的目录里，Docker容器安装的Jenkins才能读取到,<font color="orange"><strong>我这里是宿主机的/opt/jenkins挂载到了Jenkins容器里的/var/jenkins_home目录上，所以我只要解压到宿主机的/opt/jenkins目录中就可以了</strong></font></p>
<h4 id="Jenkins配置全局工具"><a href="#Jenkins配置全局工具" class="headerlink" title="Jenkins配置全局工具"></a>Jenkins配置全局工具</h4><p>进入　系统管理-&gt;全局工具配置-&gt;SonarQube Scanner     找到模块后点击<font color="orange">新增SonarQube Scanner</font></p>
<p><font color="orange">SONAR_RUNNER_HOME填你Jenkins这个Docker容器里的路径</font></p>
<p><img src="http://cdn.mjava.top/Fpgv2yqskGjp37mfoxHC6MGtIAPt" alt></p>
<h2 id="构建一个Maven项目"><a href="#构建一个Maven项目" class="headerlink" title="构建一个Maven项目"></a>构建一个Maven项目</h2><p>网上很多教程说要勾上这个选项：</p>
<p><img src="http://cdn.mjava.top/FsSrQCDF5O9x4sufLZkFRz40toHI" alt><br>其实这个是可选的，下面有一句话：<font color="orange">These variables are useful when configuring a SonarQube analysis using standard build steps such as Maven, Gradle, Ant, and command line scripts.This feature is not needed if you’re using “SonarQube Scanner” or “SonarScanner for MSBuild” build steps.</font></p>
<p>翻译过来就是：<img src="http://cdn.mjava.top/FrM7HmTx_APStJMI0OHlMQO6WL3W" alt><br>因为我们这里用的就是<font color="orange">SonarQube Scanner</font>,所以这个我们是可以不用勾上的，但是勾上也没影响；</p>
<h3 id="开始构建"><a href="#开始构建" class="headerlink" title="开始构建"></a>开始构建</h3><ul>
<li>具体怎么构建项目可以看：<a href="https://mjava.top/jenkins/build-jenkins-mavne/">Jenkins教程-创建Maven项目</a>,这里就不多介绍了</li>
</ul>
<h4 id="添加Execute-SonarQube-Scanner"><a href="#添加Execute-SonarQube-Scanner" class="headerlink" title="添加Execute SonarQube Scanner"></a>添加Execute SonarQube Scanner</h4><p>在原来构建的基础上加上<font color="orange">Execute SonarQube Scanner</font>，就可以了</p>
<p><img src="http://cdn.mjava.top/FgZ021lPaTWzgOPsG7veK1cn7lIB" alt></p>
<p>在<font color="orange">Analysis properties</font>里填上构建的参数</p>
<p><img src="http://cdn.mjava.top/FiFA65-xUZCh62Y5HfIgxQzvMx7D" alt></p>
<blockquote>
<p>唯一的项目标识符（必填）<br>sonar.projectKey =tiny-config1</p>
<p>项目元数据（以前是必需的，自SonarQube 6.1起是可选的）<br>sonar.projectName =tiny-config1<br>sonar.projectVersion = 1.0</p>
<p>源目录的路径（必需）<br>sonar.sources = srcDir1，srcDir2</p>
<p>测试源目录的路径（可选）<br>sonar.tests = testDir1，testDir2</p>
<p>Java项目编译类的路径（可选）<br>sonar.java.binaries = bin</p>
<p>逗号分隔的库路径列表（可选）<br>sonar.java.libraries = path / to / library.jar，path / to / classes / dir</p>
<p>附加参数<br>sonar.my.property =value</p>
</blockquote>
<p>保存后就可以正常构建了．</p>
<h4 id="错误解决（没有错误可跳过这段）"><a href="#错误解决（没有错误可跳过这段）" class="headerlink" title="错误解决（没有错误可跳过这段）"></a>错误解决（没有错误可跳过这段）</h4><p>如果在构建项目的时候,Jenkins控制台如果报一下错误，这是因为SonarQube的Java版本太低造成的<br><img src="http://cdn.mjava.top/FnTRL4pSwrKk5Xwbzobo89VKHf5m" alt></p>
<h5 id="升级SonarQube的Java版本"><a href="#升级SonarQube的Java版本" class="headerlink" title="升级SonarQube的Java版本"></a>升级SonarQube的Java版本</h5><p>进入SonarQube的管理台： Administration-&gt;Marketplace-&gt;SonarJava</p>
<blockquote>
<p>如果你版本没升级，右边会有个update按钮，点击就可以升级了，升级完后重启SonarQube;这边因为我已经升级过了，所以没有这个按钮</p>
</blockquote>
<p><img src="http://cdn.mjava.top/FgbCXvUiteJ9iGCCYC6qbInX0VSX" alt></p>
<h3 id="构建完成后"><a href="#构建完成后" class="headerlink" title="构建完成后"></a>构建完成后</h3><p>Jenkins控制台显示SUCCESS就表示构建成功了</p>
<p><img src="http://cdn.mjava.top/FoijfnBHiJAZ7lA7YqmdQb-bEqUF" alt></p>
<p>这时候就可以点击构建项目的SonarQube直接跳转到SonarQube控制台了</p>
<p><img src="http://cdn.mjava.top/FhXgRD7rBp5RBc2D6uBmH5mIBNKJ" alt></p>
<p>这里就可以看到结果了</p>
<p><img src="http://cdn.mjava.top/FkPydWtLuuliKAq1hc8Ex45nnTlu" alt></p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>到这里就可以根据SonarQube的提示区改BUG了．这BUG有点多＝＿＝！.</p>
<p>在搭建过程中，最主要的就是那个SonarQube Scanner这个的安装了，因为Jenkins都是Docker化的，所以他可以选择自动安装，但是我这边选择自动安装却没用，所以就自己下载了SonarQube Scnner的包挂载到Jenkins容器里区，然后直接指定SonarQube Scnner的目录就可以了；</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  


  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/3/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/3/">3</a><span class="page-number current">4</span><a class="page-number" href="/page/5/">5</a><a class="page-number" href="/page/6/">6</a><a class="extend next" rel="next" href="/page/5/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">灰灰</p>
  <div class="site-description" itemprop="description">浅水是喧哗的，深水是沉默的</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">59</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">16</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">56</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/greycode9" title="GitHub → https://github.com/greycode9" rel="noopener external nofollow noreferrer" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:zhengminghui99@gmail.com" title="E-Mail → mailto:zhengminghui99@gmail.com" rel="noopener external nofollow noreferrer" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://www.yuque.com/huihui-r7gwx" title="https://www.yuque.com/huihui-r7gwx" rel="noopener external nofollow noreferrer" target="_blank">语雀</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn" rel="noopener external nofollow noreferrer" target="_blank">赣ICP备17003482号 </a>
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">灰灰</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener external nofollow noreferrer" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org" class="theme-link" rel="noopener external nofollow noreferrer" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/muse.js"></script>
<script src="/js/next-boot.js"></script>



  




  <script src="/js/local-search.js"></script>












  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
          load: ['[tex]/mhchem'],
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
          packages: {'[+]': ['mhchem']},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script><!-- hexo-inject:begin --><!-- hexo-inject:end -->

    

  

</body>
</html>
